#!/usr/bin/env python
# coding: utf-8

"""
Created on 10/10/2011

@author: Rubén López
@author: Alfredo Campuzano
"""

from compiler import DefinitionToRegex, RegexToNFA, NFAtoDFA, MinimizeDFA, DFAtoBNF
from automata import NFA

#print "\nTesting regular definition traslator\n"
#
#alphabet = ["a", "b", "0", "1", "2"]
#
#regdef = '''
#letter = a | b;
#digit = 0 | 1 | 2;
#id = [letter]([letter]|[digit])*
#'''
#
#d = DefinitionToRegex(regdef, alphabet)
#print regdef
#print d.traslate()
#print d.definitions
#d.draw("regdef1")
#d.draw("regdef1")
#print d.errors
#
#print ""
#
#d.regdef = '''
#letter a = a;
#letter b = b;
#digit = 0 | 1 | 2;
#id = [letter a]([letter b]|[digit])*
#'''
#
#print d.regdef
#print d.traslate()
#print d.definitions
#d.draw("regdef2")
#d.draw("regdef2")
#print d.errors
#
#print "\nTesting nfa\n"
#
#states = [0,1,2,3,4,5]
#alphabet = ["a","b","c"]
#nfa = NFA(initial=0, final=5, states=states, alphabet=alphabet)
#nfa.add_transition(0,1,"c")
#nfa.add_transition(0,2,"c")
#nfa.add_transition(1,2,"a")
#nfa.add_transition(2,5,"c")
#nfa.add_transition(3,4,"b")
#nfa.add_transition(4,5,"c")
#print states
#print alphabet
#print nfa
#nfa.draw("nfa1")
#
#print ""
#
#states=["A","B","C","D","E","F"]
#alphabet=["a","b","c"]
#nfa = NFA(initial="A", final="F", states=states, alphabet=alphabet)
#nfa.add_transition("A","B","c")
#nfa.add_transition("A","C","c")
#nfa.add_transition("B","C","a")
#nfa.add_transition("C","F","c")
#nfa.add_transition("D","E","b")
#nfa.add_transition("E","F","c")
#print states
#print alphabet
#print nfa
#nfa.draw("nfa2")
#
#print ""
#
#states=[]
#alphabet=["a","b","c"]
#nfa = NFA(initial="A", final="F", states=states, alphabet=alphabet)
#nfa.add_transition("A","B","c")
#nfa.add_transition("A","C","c")
#nfa.add_transition("B","C","a")
#nfa.add_transition("C","F","c")
#nfa.add_transition("D","E","b")
#nfa.add_transition("E","F","c")
#print states
#print alphabet
#print nfa
#nfa.draw("nfa3")
#
#print "\nTesting thompson\n"
#
#thompson = RegexToNFA("ab.", ["a", "b"])
#nfa = thompson.traslate()
#print thompson.exp
#print nfa
#nfa.draw("thompson1")
#
#print ""
#
#thompson.exp = "ab|"
#nfa = thompson.traslate()
#print thompson.exp
#print nfa
#nfa.draw("thompson2")
#
#thompson.exp = "ab.ba|.a."
#nfa = thompson.traslate()
#print thompson.exp
#print nfa
#nfa.draw("thompson3")
#
#d = DefinitionToRegex("exp = (a|b)*a+b?b;", ["a", "b"])
#thompson.exp = d.traslate()
#nfa = thompson.traslate()
#print thompson.exp
#print nfa
#nfa.draw("nfa")
#
#print "\nTesting nfa_to_dfa"
#
#n2d = NFAtoDFA()
#dfa = n2d.convertNFAtoDFA(nfa)
#print dfa
#dfa.draw("dfa")
#print dfa.accepts("aabb", "dfa")
#
#mini = MinimizeDFA()
#dfa = mini.remove_dead(dfa)
#dfa = mini.remove_inac(dfa)
#dfa = mini.minimize(dfa)
#dfa.draw("mindfa")
#print dfa.accepts("aabb", "mindfa")

print "\nTesting all"

alphabet = ["a", "b", "c",
            "0", "1", "2",
            "E", ",", "-"]

regdefs = []

#regdef1 = '''
#letter = a|b|c;
#digit = 0|1|2;
#id = [letter]([letter]|[digit])*;
#'''
#
#regdef2 = '''
#digit = 0|1|2;
#digits = [digit][digit]*;
#fraction = ,[digits];
#exponent = E-?[digits];
#number = [digits][fraction]?[exponent]?;
#'''

regdef1 = '''
exp = (a|b)*
'''

regdefs.append(regdef1)
#regdefs.append(regdef2)
i = 1

for regdef in regdefs:

    alphabet1 = []
    alphabet1.extend(alphabet)

#    print "\nregular definition:\n\n", regdef

    d = DefinitionToRegex(regdef, alphabet1)
    postfix = d.traslate()

#    print "\npostfix notation: ", postfix

    t = RegexToNFA(postfix, alphabet1)
    nfa = t.traslate()

#    print "\nnfa notation:\n\n", nfa
#
#    filename = "all" + str(i)
#    filename1 = filename + ".dfa"
#    filename2 = filename + ".mindfa"
#
#    d.draw(filename)
#    d.draw(filename=filename, flag=True)

    n2d = NFAtoDFA()
    dfa = n2d.convertNFAtoDFA(nfa)
#    print dfa.accepts("abc012", filename1)
#    print dfa.accepts("10,21E-11", filename1)
#    dfa.draw(filename1)
    dfa.__str__()

    mini = MinimizeDFA()
#    dfa = mini.remove_dead(dfa)
#    dfa = mini.remove_inac(dfa)
    dfa = mini.minimize(dfa)
    print dfa
#    print dfa.accepts("abc012", filename2)
#    print dfa.accepts("10,21E-11", filename2)
#    dfa.draw(filename2)
#
#    i = i + 1
#
#    b = DFAtoBNF()
#    bnf = b.convert(dfa)
#
#    print "BNF:\n"
#
#    print bnf

